<?php
/**
 * Model PDO Driver - PgSQL
 *
 * @package model
 * @subpackage pdo
 *
 * @author jylee3@simplexi.com
 * @version 1.0
 */
class coreModelDbDriverPgsql extends coreModelDbDriverCommon
{
    public function __construct($dsn, $username, $passwd, $options)
    {
        parent::__construct($dsn, $username, $passwd, $options);

        $this->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, true);
    }

    /**
     * Information Encrypt Function
     *
     * @param    Mixed    $mData
     * @param    String    $sKey
     * @param    String    $sIV
     *
     * @return    String
     */
    public function encrypt($mData, $sKey, $sIV='')
    {
        return "encode(encrypt_iv('".trim($mData)."','".$sKey."','".$sIV."','AES'),'hex')";
    }

    /**
     * Information Decrypt Function
     *
     * @param    Mixed    $sColumnName
     * @param    String    $sKey
     * @param    String    $sIV
     * @param    Booelean    $bIsValue
     *
     * @return    String
     */
    public function decrypt($sColumnName, $sKey, $sIV='', $bIsValue=false, $sCharset='UTF-8')
    {
        $sDecodeFunction = ($bIsValue===false)? "decode(".trim($sColumnName).",'hex')" : "decode('".trim($sColumnName)."','hex')";

        return "convert_from(decrypt_iv(".$sDecodeFunction.",'".$sKey."','".$sIV."','AES'),'$sCharset')";
    }

    /**
     * SQL Escape
     *
     * @param    String    $sSql
     *
     * @return    String
     */
    public function escapeString($sSql)
    {
        return (function_exists('pg_escape_string'))? pg_escape_string($sSql) : parent::escapeString($sSql);
    }
}
